//
//  MieDianViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/11.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "MieDianViewController.h"

@interface MieDianViewController ()

@property (nonatomic, strong) UIImageView *peopleImage;
@property (nonatomic, strong) UIView *backgroudView;

@end

@implementation MieDianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.backgroudView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backgroudView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_backgroudView];
    
    self.peopleImage = [[UIImageView alloc] initWithFrame:CGRectMake(30, 50, 150, 150)];
    self.peopleImage.image = [UIImage imageWithName:@"snowMan"];
    self.peopleImage.backgroundColor = [UIColor redColor];
    self.peopleImage.userInteractionEnabled = YES;
    [self.backgroudView addSubview:_peopleImage];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTap:)];
    [self.peopleImage addGestureRecognizer:tapGes];
    
#pragma mark - 灭点的定义：当在透视角度绘图的时候，远离相机视角的物体将会变小变远，当远离到一个极限距离，它们可能就缩成了一个点，于是所有的物体最后都汇聚消失在同一个点。这个点就是灭点
    // Core Animation定义了这个点位于变换图层的anchorPoint。这就是说，当图层发生变换时，这个点永远位于图层变换之前anchorPoint的位置，就是说anchorPoint就是灭点。
   // 当改变一个图层的position，你也改变了它的灭点，做3D变换的时候要时刻记住这一点，当你视图通过调整⚠️m34来让它更加有3D效果，应该首先把它放置于屏幕中央，然后通过平移来把它移动到指定位置（而不是直接改变它的position），这样所有的3D图层都共享一个灭点。⚠️设置M34的时候，灭点就确定了，灭点一般只有在做3D变化的时候才会用到，2D变化不需要灭点。
    // 补充：subLayerTransform可以为子图层设置相同的M34，即灭点都位于父视图的中心点。这个示例代码在下一个控制器中。
    
    // 在现实中，灭点通常是视图的中心，于是为了在应用中创建拟真效果的透视，这个点应该聚在屏幕中点，或者至少是包含所有3D对象的视图中点。⚠️因此在实际写代码的过程中要模拟真实情况，在做3D变化的时候应该将所有视图的position(灭点)都置于视图的中心，然后通过layer的transform做3D变化来达到预期的效果，这样所有的视图就会共享同一个灭点，即屏幕的中心点。2D变化不需要灭点。
    
    /**
     *  ⚠️⚠️⚠️结论：视图的position和center始终是重合的，并且一旦视图初始化完毕就不会在改变，除非手动改变positon或center或frame，即通过view的transform或者afffineTransform或者layer的transform达到的视图变化不会影响postion和center；frame即视图的可视范围在哪里就是哪里，是会变的;
     
     
     */
    
    UIButton *changeTransformbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeTransformbutton.frame = CGRectMake(0, 300, 375, 30);
    changeTransformbutton.backgroundColor = [UIColor cyanColor];
    [changeTransformbutton setTitle:@"改变View的Transform" forState:UIControlStateNormal];
    [changeTransformbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeTransformbutton addTarget:self action:@selector(changeTransformbutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroudView addSubview:changeTransformbutton];
    
    UIButton *changeAffineTransformbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeAffineTransformbutton.frame = CGRectMake(0, 330, 375, 30);
    changeAffineTransformbutton.backgroundColor = [UIColor cyanColor];
    [changeAffineTransformbutton setTitle:@"改变AffineTransform" forState:UIControlStateNormal];
    [changeAffineTransformbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeAffineTransformbutton addTarget:self action:@selector(changeAffineTransformbutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroudView addSubview:changeAffineTransformbutton];
    
    UIButton *changeLayerTransformbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeLayerTransformbutton.frame = CGRectMake(0, 360, 375, 30);
    changeLayerTransformbutton.backgroundColor = [UIColor cyanColor];
    [changeLayerTransformbutton setTitle:@"改变Layer的Transform" forState:UIControlStateNormal];
    [changeLayerTransformbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeLayerTransformbutton addTarget:self action:@selector(changeLayerTransformbutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroudView addSubview:changeLayerTransformbutton];
    
    UIButton *changeLayerPositionbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeLayerPositionbutton.frame = CGRectMake(0, 390, 375, 30);
    changeLayerPositionbutton.backgroundColor = [UIColor cyanColor];
    [changeLayerPositionbutton setTitle:@"改变Layer的Position" forState:UIControlStateNormal];
    [changeLayerPositionbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeLayerPositionbutton addTarget:self action:@selector(changeLayerPositionbutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroudView addSubview:changeLayerPositionbutton];
    
    UIButton *changeViewCenterbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeViewCenterbutton.frame = CGRectMake(0, 420, 375, 30);
    changeViewCenterbutton.backgroundColor = [UIColor cyanColor];
    [changeViewCenterbutton setTitle:@"改变View的Center" forState:UIControlStateNormal];
    [changeViewCenterbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeViewCenterbutton addTarget:self action:@selector(changeViewCenterbutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroudView addSubview:changeViewCenterbutton];
 
    UIButton *changeLayerAnchorPointbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeLayerAnchorPointbutton.frame = CGRectMake(0, 450, 375, 30);
    changeLayerAnchorPointbutton.backgroundColor = [UIColor cyanColor];
    [changeLayerAnchorPointbutton setTitle:@"改变layer的AnchorPoint" forState:UIControlStateNormal];
    [changeLayerAnchorPointbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeLayerAnchorPointbutton addTarget:self action:@selector(changeLayerAnchorPointbutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroudView addSubview:changeLayerAnchorPointbutton];
    
    UIButton *changeViewFramebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeViewFramebutton.frame = CGRectMake(0, 480, 375, 30);
    changeViewFramebutton.backgroundColor = [UIColor cyanColor];
    [changeViewFramebutton setTitle:@"改变View的Frame" forState:UIControlStateNormal];
    [changeViewFramebutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeViewFramebutton addTarget:self action:@selector(changeViewFramebutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroudView addSubview:changeViewFramebutton];
    
  //  [[UIImage imageNamed:@""] drawAtPoint:CGPointZero];

    UIButton *recoverbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    recoverbutton.frame = CGRectMake(0, 510, 375, 30);
    recoverbutton.backgroundColor = [UIColor cyanColor];
    [recoverbutton setTitle:@"回复原状" forState:UIControlStateNormal];
    [recoverbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [recoverbutton addTarget:self action:@selector(recoverbutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroudView addSubview:recoverbutton];

 
}

- (void)changeTransformbutton:(UIButton *)button
{
    // 直接对peopleImage的View的transform最变换，来查看position的变化
    [self changePeopleImageViewTransform];
}

- (void)changeAffineTransformbutton:(UIButton *)button
{
    // 改变peopleImage的affineTransform，来查看position的变化
        [self changePeopleImageAffineTransform];
}

- (void)changeLayerTransformbutton:(UIButton *)button
{
    //    // 改变peopleImage的Layer的tranform，来查看position的变化
        [self changePeopleImageLayerTransform];
}

- (void)changeLayerPositionbutton:(UIButton *)button
{
    // 改变peopleImage的postion，来查看影响的值
       [self changePeopleImagePositon];
}

- (void)changeViewCenterbutton:(UIButton *)button
{
    // 改变peopleImage的center，来查看影响的值
       [self changePeopleImageCenter];
}

- (void)changeLayerAnchorPointbutton:(UIButton *)button
{
    // 改变peopleImage的anchorPoint，来查看影响的值
        [self changePeopleImageAnchorPositoin];
}

- (void)changeViewFramebutton:(UIButton *)button
{
    // 改变peopleImage的frame，来查看影响的值
        [self changePeopleImageFrame];
}

- (void)recoverbutton:(UIButton *)button
{
    self.peopleImage.frame = CGRectMake(30, 50, 150, 150);
}

- (void)clickTap:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"点击了");
}

// 直接对peopleImage的View的transform最变换，来查看position的变化
- (void)changePeopleImageViewTransform
{
    NSLog(@"peopleImage改变view的transform之前，frame：%@，center：%@，postion：%@，锚点：%@", [NSValue valueWithCGRect:self.peopleImage.frame], [NSValue valueWithCGPoint:self.peopleImage.center], [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint:self.peopleImage.layer.anchorPoint]);
    self.peopleImage.transform = CGAffineTransformMakeTranslation(100, 0);
     NSLog(@"peopleImage改变view的transform之后，frame：%@，center：%@，postion：%@，锚点：%@", [NSValue valueWithCGRect:self.peopleImage.frame], [NSValue valueWithCGPoint:self.peopleImage.center], [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint:self.peopleImage.layer.anchorPoint]);
}

// 改变peopleImage的affineTransform，来查看position的变化
- (void)changePeopleImageAffineTransform
{
    NSLog(@"peopleImage改变Layer的AffineTransform之前，frame：%@，center：%@，postion：%@，锚点：%@", [NSValue valueWithCGRect:self.peopleImage.frame], [NSValue valueWithCGPoint:self.peopleImage.center], [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint:self.peopleImage.layer.anchorPoint]);
    self.peopleImage.layer.affineTransform = CGAffineTransformMakeTranslation(100, 0);
    NSLog(@"peopleImage改变Layer的AffineTransform之后，frame：%@，center：%@，postion：%@，锚点：%@", [NSValue valueWithCGRect:self.peopleImage.frame], [NSValue valueWithCGPoint:self.peopleImage.center], [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint:self.peopleImage.layer.anchorPoint]);
}

// 改变peopleImage的Layer的tranform，来查看position的变化
- (void)changePeopleImageLayerTransform
{
    NSLog(@"peopleImage改变Layer的transform之前，frame：%@，center：%@，postion：%@，锚点：%@", [NSValue valueWithCGRect:self.peopleImage.frame], [NSValue valueWithCGPoint:self.peopleImage.center], [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint:self.peopleImage.layer.anchorPoint]);
    self.peopleImage.layer.transform = CATransform3DMakeTranslation(100, 100, 100); // 想右下角移动，Z轴向我们移动100
    NSLog(@"peopleImage改变Layer的transform之后，frame：%@，center：%@，postion：%@，锚点：%@", [NSValue valueWithCGRect:self.peopleImage.frame], [NSValue valueWithCGPoint:self.peopleImage.center], [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint:self.peopleImage.layer.anchorPoint]);
}

// 改变peopleImage的postion，来查看影响的值
- (void)changePeopleImagePositon
{
    NSLog(@"改变position之前，peopleImage2的position:%@,锚点：%@, peopleImage2的center：%@，peopleImage2的frame：%@", [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint: self.peopleImage.layer.anchorPoint], [NSValue valueWithCGPoint:self.peopleImage.center],  [NSValue valueWithCGRect:self.peopleImage.frame]);
    self.peopleImage.layer.position = CGPointMake(250, 350);
    NSLog(@"改变position之后，peopleImage2的position:%@,锚点：%@, peopleImage2的center：%@，peopleImage2的frame：%@", [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint: self.peopleImage.layer.anchorPoint], [NSValue valueWithCGPoint:self.peopleImage.center],  [NSValue valueWithCGRect:self.peopleImage.frame]);

}

// 改变peopleImage的center，来查看影响的值
- (void)changePeopleImageCenter
{
    NSLog(@"改变Center之前，peopleImage2的position:%@,锚点：%@, peopleImage2的center：%@，peopleImage2的frame：%@", [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint: self.peopleImage.layer.anchorPoint], [NSValue valueWithCGPoint:self.peopleImage.center],  [NSValue valueWithCGRect:self.peopleImage.frame]);
    self.peopleImage.center = CGPointMake(250, 350);
    NSLog(@"改变Center之后，peopleImage2的position:%@,锚点：%@, peopleImage2的center：%@，peopleImage2的frame：%@", [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint: self.peopleImage.layer.anchorPoint], [NSValue valueWithCGPoint:self.peopleImage.center],  [NSValue valueWithCGRect:self.peopleImage.frame]);
}

// 改变peopleImage的anchorPoint，来查看影响的值
- (void)changePeopleImageAnchorPositoin
{
    NSLog(@"改变anchorPoint之前，peopleImage2的position:%@,锚点：%@, peopleImage2的center：%@，peopleImage2的frame：%@", [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint: self.peopleImage.layer.anchorPoint], [NSValue valueWithCGPoint:self.peopleImage.center],  [NSValue valueWithCGRect:self.peopleImage.frame]);
    self.peopleImage.layer.anchorPoint = CGPointMake(0, 0);
    NSLog(@"改变anchorPoint之后，peopleImage2的position:%@,锚点：%@, peopleImage2的center：%@，peopleImage2的frame：%@", [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint: self.peopleImage.layer.anchorPoint], [NSValue valueWithCGPoint:self.peopleImage.center],  [NSValue valueWithCGRect:self.peopleImage.frame]);
}

// 改变peopleImage的frame，来查看影响的值
- (void)changePeopleImageFrame
{
    NSLog(@"改变Frame之前，peopleImage2的position:%@,锚点：%@, peopleImage2的center：%@，peopleImage2的frame：%@", [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint: self.peopleImage.layer.anchorPoint], [NSValue valueWithCGPoint:self.peopleImage.center],  [NSValue valueWithCGRect:self.peopleImage.frame]);
    self.peopleImage.frame= CGRectMake(100, 100, 100, 100);
    NSLog(@"改变Frame之后，peopleImage2的position:%@,锚点：%@, peopleImage2的center：%@，peopleImage2的frame：%@", [NSValue valueWithCGPoint:self.peopleImage.layer.position], [NSValue valueWithCGPoint: self.peopleImage.layer.anchorPoint], [NSValue valueWithCGPoint:self.peopleImage.center],  [NSValue valueWithCGRect:self.peopleImage.frame]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
